import * as echarts from '../../ec-canvas/echarts';

const app = getApp();


function setOption(chart) {
  const option = {
    title: {
      text: '指定血压数据(A舒张压B收缩压)',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis'
    },
    color: ["#37A2DA", "#67E0E3"],
    legend: {
      data: ['A', 'B'],
      top: 30,
      left: 'center',
      backgroundColor: 'red',
      z: 100
    },
    grid: {
      containLabel: true
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      interval: 0,
      data: app.globalData.recTimeData,
      // show: false
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
      // show: false
    },
    series: [{
      name: 'A',
      type: 'line',
      smooth: true,
      data: app.globalData.szyData
    }, {
      name: 'B',
      type: 'line',
      smooth: true,
        data: app.globalData.ssyData
    }]
  };
  chart.setOption(option);
}

Page({
  data: {
    recTimeData: [],
    szyData: [],
    ssyData: [],
    ec: {
      lazyLoad: true,
      // onInit: initChart
    },
    isLoaded: false,
    isDisposed: false
  },
  init: function () {
    this.ecComponent.init((canvas, width, height) => {
      // 获取组件的 canvas、width、height 后的回调函数
      // 在这里初始化图表
      const chart = echarts.init(canvas, null, {
        width: width,
        height: height
      });
      setOption(chart);
      // 将图表实例绑定到 this 上，可以在其他成员函数（如 dispose）中访问
      this.chart = chart;
      this.setData({
        isLoaded: true,
        isDisposed: false
      });
      // 注意这里一定要返回 chart 实例，否则会影响事件处理等
      return chart;
    });
  },

  onLoad: function (options){
    //查询数据进行加载
    var type = options.type;
    //通过类型查询不同的值
    
  
  },
  onReady: function () {
    // 获取组件
    this.ecComponent = this.selectComponent('#mychart-dom-bar');
  },
  dispose: function () {
    if (this.chart) {
      this.chart.dispose();
    }

    this.setData({
      isDisposed: true
    });
  }
});
